/**************************************************************************
 *
 * Description : hb_auto_gpio.c
 * Author      : isen
 * Date        : 2021.05.26
 * Verson      : V1.0
 * Function    : 语音台灯
 *               1，不带通讯功能
 *               2，控制2路pwm灯光切换渐变
 *               3，读取2路io信号，触摸按键，开关，模式键
 *               
 *              功能
 *            1，模式按键，弹起有效，白光-》黄光-》黄白光-》白光 循环
 *            2，开关按键，按下有效，开-》关-》开 循环
 *            3，长安模式键，首次开机增加亮度，再次长按降低亮度
 *            4，掉电记忆功能
 **************************************************************************/
#include "user_config.h"
#include "user_event.h"
#include "user_gpio.h"
#include "user_player.h"
#include "user_pwm.h"
#include "user_timer.h"
#include "user_uart.h"
#include "uni_hal_timer.h"
#include "user_flash.h"
#include "uni_audio_player.h"
#include "user_asr.h"
#include "yfc_auto_gpio.c"
#include "yfc_parm.h"
#include "user_flash.h"

#define CMD_OPEN          "auto1"
#define CMD_CLOSE         "auto2"
#define CMD_LUME_UP       "auto3"
#define CMD_LUME_DOWN     "auto4"
#define CMD_LUMA_MAX      "auto5"
#define CMD_LUME_MIN      "auto6"
#define CMD_LUME_HALF     "auto7"
#define CMD_COOL          "auto8"
#define CMD_WARM          "auto9"
#define CMD_NATURE        "auto10"
#define CMD_SLEEP         "auto11"
#define CMD_WORK          "auto12"
#define CMD_READ          "auto15"
#define CMD_BREAK         "auto14"

#define MAX_PWM           (uni_u8)100
#define MID_PWM           (uni_u8)50
#define MIN_PWM           (uni_u8)5
#define INC_PWM           (uni_u8)10

#define BLOB_TYPE_U32  uni_u32

#define LED_YELLOW_NUM      PWM_NUM_1_B2  //PWM_NUM_2_A28 //PWM_NUM_1_B2 
#define LED_WHITE_NUM       PWM_NUM_2_B3  //PWM_NUM_1_A27 //PWM_NUM_2_B3
#define KEY_SW_NUM          GPIO_NUM_A27  //GPIO_NUM_A26  //GPIO_NUM_A27


#define MODE_WHITE     0
#define MODE_YELLOW    1
#define MODE_NATURE    2
#define MODE_BREATHE   3

#define ONEHOUR       10//36000
#define TENMINUTE     5//600

#define LED_OFF       0
#define LED_ON        1

#define SYS_VOLUME_SET      (uni_u16)0X005A  //音量控制
#define SYS_PLAY_FB         (uni_u16)0X005B  //播报反馈(主动 手动)
#define SYS_FUN_SEL         (uni_u16)0X005C  //功能开关（开 关）
#define SYS_WAITUP_SEL      (uni_u16)0X005D  //识别屏蔽（）
#define SYS_DELAY_PA        (uni_u16)0X005E  //功放延时（）
#define SYS_PLAY_LIST       (uni_u16)0X005F  //主动播报选择（随机 对应）

#define PLAY_FB_VOLUME    (uni_u8)0X01
#define PLAY_FB_WAITUP    (uni_u8)0X02
#define PLAY_FB_CUSTOM    (uni_u8)0X03
#define PLAY_FB_EXIT      (uni_u8)0X04
#define PLAY_FB_MUTE      (uni_u8)0X05
#define PLAY_FB_ONCE      (uni_u8)0X06

#define AUTO_PLAY       (uni_u8)0x80
#define FUN_EN          (uni_u8)0x40
#define AUTO_LIST       (uni_u8)0x20

#define VOL_MAX     (uni_u16)0x0006
#define VOL_MID     (uni_u16)0x0008
#define VOL_MIN     (uni_u16)0x0007
#define VOL_UP      (uni_u16)0x0004
#define VOL_DOWN    (uni_u16)0x0005

#define ADDR_SYS_MIN      (uni_u16)0x005a
#define ADDR_SYS_MAX      (uni_u16)0x0095

#define ADDR_WAITUP_MIN      (uni_u16)0x001e
#define ADDR_WAITUP_MAX      (uni_u16)0x0027


#define BLOB_COLOR   "SAVE_COLOR"     //颜色
#define BLOB_LUMEN   "SAVE_LUMEN"     //亮度

typedef struct{
  uni_u8 ledStatus;//0:close 1:open
  uni_u8 lumen;
  uni_u8 lsLumen;
  uni_u8 pwmMaster;//10-100
  uni_u8 wPwm;//白光pwm当时值
  uni_u8 yPwm;//黄光pwm当时值
  uni_u8 ledMode;//0:w 1:y 2:w & y
  uni_u8 ledModeLast;
  uni_u8 lumeDir;//0:down 1:up
  uni_u8 bLong; //long key flag
  uni_u8 modeChange;
  uni_u8 bLume;
  uni_u8 bBreathe;
  uni_u8 keepEye;
  uni_u8 keepEyeMode;//0-学习时间  1-休息时间
  uni_u32 cntKeepEye;//计时器 1小时全亮 10分钟最暗
  uni_u8 bVoiceEn;//语音控制使能位  只有用语音开了灯,才可以用语音控制灯
}LedParm;


LedParm ledParm = {0};
uni_u32 cntTkDelay = 0;

void _timer_cb(TIMER_INDEX TimerIdx);
void setLedLume(uni_u8 lumen);
int user_flash_get(void);
int user_flash_set(void);
int user_parm_init(void);

static void _costom_waitup_cb(USER_EVENT_TYPE event, user_event_context_t *context) {
  event_goto_awakend_t *waitup = NULL;
  unsigned int i = 0, serial;

  if (context) {
    waitup = &context->goto_awakend;
    printf("_costom_waitup_cb");

    user_player_reply_list_random(waitup->reply_files);

    // user_gpio_set_value(GPIO_NUM_A26, 0);
  }
}

static void _custom_setting_cb(USER_EVENT_TYPE event, user_event_context_t *context) {

  event_custom_setting_t *setting = NULL;
  unsigned int i = 0;
  
  if(context){
    setting = &context->custom_setting;
    printf("user command aa: %s %s\n", setting->cmd, setting->reply_files);

  
    if (0 == uni_strcmp(setting->cmd, CMD_OPEN)){        //开灯@好的
      if(ledParm.ledStatus==1){
        
      }else{
        ledParm.ledStatus = 1;//开灯
        setLedAllOn();
      }
           
    }else if (0 == uni_strcmp(setting->cmd, CMD_CLOSE)){  // 关灯@好的
      printf("off light\n");
      if(ledParm.ledStatus==1){
        ledParm.ledStatus = 0;//关灯

        ledParm.ledModeLast = ledParm.ledMode;
        ledParm.lsLumen = ledParm.lumen;

        setLedAllOff();
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_NATURE)){  // 自然光@好的
      if(ledParm.ledStatus!=1){
        // return;
        ledParm.ledStatus = 1;
      }
      if(MODE_NATURE != ledParm.ledMode){
        ledParm.ledModeLast = ledParm.ledMode;

        ledParm.ledMode = MODE_NATURE;
        setLedModeChange();
        user_flash_set();
      }

    }else if (0 == uni_strcmp(setting->cmd, CMD_COOL)){// 白光@好的
      if(ledParm.ledStatus!=1){
        // return;
        ledParm.ledStatus = 1;
      }
      if(MODE_WHITE != ledParm.ledMode){
        ledParm.ledModeLast = ledParm.ledMode;
        ledParm.ledMode = MODE_WHITE;
        setLedModeChange();
        user_flash_set();
      }

    }else if (0 == uni_strcmp(setting->cmd, CMD_WARM)){// 黄光@好的
      if(ledParm.ledStatus!=1){
        // return;
        ledParm.ledStatus = 1;
      }
      if(MODE_YELLOW != ledParm.ledMode){
        ledParm.ledModeLast = ledParm.ledMode;
        ledParm.ledMode = MODE_YELLOW;
        setLedModeChange();
        user_flash_set();
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_LUMA_MAX)){// 最亮@好的
      if(ledParm.ledStatus!=1){
        return;
      }
      if(ledParm.pwmMaster != MAX_PWM){
        if(ledParm.pwmMaster < MAX_PWM){
          ledParm.pwmMaster = MAX_PWM;
          setLedLume(MAX_PWM);
          user_flash_set();
        }
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_LUME_MIN)){// 最暗@好的
      if(ledParm.ledStatus!=1){
        return;
      }
      if(ledParm.pwmMaster != MIN_PWM){
        if(ledParm.pwmMaster > MIN_PWM){
          ledParm.pwmMaster = MIN_PWM;
          setLedLume(MIN_PWM);
          user_flash_set();
        }
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_LUME_HALF)){// 中间亮度@好的
      if(ledParm.ledStatus!=1){
        return;
      }
      if(ledParm.pwmMaster != MID_PWM){
        if(ledParm.pwmMaster > MID_PWM){  
          ledParm.pwmMaster = MID_PWM;
          setLedLume(MID_PWM);
          user_flash_set();
        }else{
          ledParm.pwmMaster = MID_PWM;
          setLedLume(MID_PWM);
          user_flash_set();
        }
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_LUME_UP)){// 亮一点
      if(ledParm.ledStatus!=1){
        return;
      }

      if(ledParm.pwmMaster < MAX_PWM){
        ledParm.pwmMaster += INC_PWM;
        if(ledParm.pwmMaster>MAX_PWM){
          ledParm.pwmMaster = MAX_PWM;
        }
        setLedLume(ledParm.pwmMaster);
        user_flash_set();
      }
      
    }else if (0 == uni_strcmp(setting->cmd, CMD_LUME_DOWN)){// 暗一点
      if(ledParm.ledStatus!=1){
        return;
      }
      if(ledParm.pwmMaster > MIN_PWM){
        ledParm.pwmMaster -= MIN_PWM;
        if(ledParm.pwmMaster<MIN_PWM){
          ledParm.pwmMaster = MIN_PWM;
        }
        setLedLume(ledParm.pwmMaster);
        user_flash_set();
      }
    }else if (0 == uni_strcmp(setting->cmd, CMD_BREAK)){//呼吸光
      if(ledParm.ledStatus!=1){
        return;
      }
      setLedModeBreathe();
    }

    user_player_reply_list_random("[114]");
  }
}

static void _costom_sleeping_cb(USER_EVENT_TYPE event,
                               user_event_context_t *context) {
  event_goto_sleeping_t *sleep = NULL;
  unsigned int i = 0;

  if(context){
    sleep = &context->goto_sleeping;
    printf("goto sleep\n");
    user_gpio_set_value(GPIO_NUM_A26, 1);
  }
}

static void _custom_audio_play_start(USER_EVENT_TYPE event, user_event_context_t *context){

  event_audio_play_t *playing = NULL;

  if (context){
    playing = &context->audio_play;
    LOGT(TAG, "_custom_audio_play_start: %s", playing->file_name);

	  user_gpio_set_value(PIN_PA, PA_EN_LV);
  }
}


static void _custom_audio_play_end(USER_EVENT_TYPE event, user_event_context_t *context) {

  event_audio_end_t *ending = NULL;

  if (context) {
    ending = &context->audio_end;

	  user_gpio_set_value(PIN_PA, PA_DIS_LV);
  }
}

static void _costom_volume_cb(USER_EVENT_TYPE event, user_event_context_t *context) {
  event_volume_setting_t *volume = NULL;
  unsigned int i = 0;
  if (context) {
    volume = &context->voluem_setting;
    printf("_costom_volume_cb %s\n", volume->cmd);
  }
}

static void _register_event_callback(void){
  // user_event_subscribe_event(USER_GOTO_AWAKENED, _costom_waitup_cb);
  // user_event_subscribe_event(USER_CUSTOM_SETTING, _custom_setting_cb);
  // user_event_subscribe_event(USER_GOTO_SLEEPING, _costom_sleeping_cb);

  user_event_subscribe_event(USER_GOTO_AWAKENED, _costom_waitup_cb);  
  user_event_subscribe_event(USER_CUSTOM_SETTING, _custom_setting_cb);
  user_event_subscribe_event(USER_AUDIO_PLAY_START, _custom_audio_play_start);
  user_event_subscribe_event(USER_AUDIO_PLAY_END, _custom_audio_play_end);
  // user_event_subscribe_event(USER_VOLUME_SETTING, _costom_volume_cb);
  user_event_subscribe_event(USER_GOTO_SLEEPING, _costom_sleeping_cb);
}


//======led control
void timeKeepEye(void)
{

  //1小时学习 灯最亮 10分钟休息 灯最暗
  if(ledParm.keepEye){

    if(ledParm.keepEyeMode){
      //工作时间
      if(ledParm.cntKeepEye){
        ledParm.cntKeepEye--;
      }else{
        ledParm.keepEyeMode = 0;
        ledParm.cntKeepEye = TENMINUTE;
        if(ledParm.pwmMaster > MIN_PWM){
          setLedLume(MIN_PWM);
          // ledParm.lumeDir = 1;
          // ledParm.pwmBuf = ledParm.pwmBuf;        
          ledParm.pwmMaster = MIN_PWM;
        }
      }
    }else{
      //休息时间
      if(ledParm.cntKeepEye){
        ledParm.cntKeepEye--;
      }else{
        ledParm.keepEyeMode = 1;
        ledParm.cntKeepEye = ONEHOUR;
        if(ledParm.pwmMaster < MAX_PWM){
          setLedLume(MAX_PWM);
          // ledParm.lumeDir = 0;
          // ledParm.pwmBuf = ledParm.pwmMaster;        
          ledParm.pwmMaster = MAX_PWM;
        }
      }
    }
    uni_hal_timer_pause(TIMER5, 1);
    uni_hal_timer_start(TIMER5);
  }

}

/*
  3-5秒呼吸周期
  最暗，最亮停顿一下，
  加入随机周期
  20ms
*/
void timeLedBreathe(void)
{
  uni_u8 bRestart = 0;
  static uni_u8 dir = 0;
  static uni_u16 cycleHalf = 200;//第一次默认4秒
  static uni_u16 cntCycle = 0;
  static uni_u8 update = 0;

  printf("timeLedBreathe %d %d\n", ledParm.wPwm, ledParm.yPwm);


  switch(ledParm.ledMode){  //0:w 1:y 2:w & y
    case 0:
      if(ledParm.wPwm==ledParm.pwmMaster){
        
      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.wPwm--;
      }

      if(ledParm.yPwm){
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    case 1:
      if(ledParm.yPwm==ledParm.pwmMaster){
        
      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.yPwm--;
      }

      if(ledParm.wPwm){
        ledParm.wPwm--;
        bRestart = 1;
      }
      break;

    case 2:
      if(ledParm.yPwm==ledParm.pwmMaster){

      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.yPwm--;
      }

      if(ledParm.wPwm==ledParm.pwmMaster){

      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.wPwm--;
      }
      break;
    default:
      return;
  }

  user_pwm_change_duty(LED_YELLOW_NUM, ledParm.yPwm);
  user_pwm_change_duty(LED_WHITE_NUM, ledParm.wPwm);

  if(bRestart){
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6);
  }else{
    ledParm.bLume = 0;
  }
} 

void timeLedLume(void){
  uni_u8 bRestart = 0;
  
  printf("timeLedLume %d %d\n", ledParm.wPwm, ledParm.yPwm);

  switch(ledParm.ledMode){  //0:w 1:y 2:w & y
    case 0:
      if(ledParm.wPwm==ledParm.pwmMaster){
        
      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.wPwm--;
      }

      if(ledParm.yPwm){
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    case 1:
      if(ledParm.yPwm==ledParm.pwmMaster){
        
      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.yPwm--;
      }

      if(ledParm.wPwm){
        ledParm.wPwm--;
        bRestart = 1;
      }
      break;

    case 2:
      if(ledParm.yPwm==ledParm.pwmMaster){

      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.yPwm--;
      }

      if(ledParm.wPwm==ledParm.pwmMaster){

      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        bRestart = 1;
        ledParm.wPwm--;
      }
      break;
    default:
      return;
  }

  printf("timeLedLume %d %d\n", ledParm.yPwm, ledParm.wPwm);

  user_pwm_change_duty(LED_YELLOW_NUM, ledParm.yPwm);
  user_pwm_change_duty(LED_WHITE_NUM, ledParm.wPwm);

  if(bRestart){
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6);
  }else{
    ledParm.bLume = 0;
  }
}


void timeLedOn(void){
  uni_u8 bRestart = 0;

  // printf("timeLedOn %d %d\n", ledParm.wPwm, ledParm.yPwm);

  switch(ledParm.ledMode){    //0:w 1:y 2:w & y
    case MODE_WHITE:
      if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }

      if(ledParm.yPwm){
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    case MODE_YELLOW:
      if(ledParm.wPwm){
        ledParm.wPwm--;
        bRestart = 1;
      }

      if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }
      break;

    case MODE_NATURE:
      if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }

      if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }
      break;
    default:
      return;
  }

  user_pwm_change_duty(LED_YELLOW_NUM, ledParm.yPwm);
  user_pwm_change_duty(LED_WHITE_NUM, ledParm.wPwm);

  if(bRestart){
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6);
  }
}

void timeLedOff(void){
  uni_u8 bRestart = 0;
  // printf("timeLedOff %d %d\n", ledParm.wPwm, ledParm.yPwm);
  //0:w 1:y 2:w & y
  if(ledParm.wPwm){
    ledParm.wPwm--;
    bRestart = 1;
    user_pwm_change_duty(LED_WHITE_NUM, ledParm.wPwm);
  }

  if(ledParm.yPwm){
    ledParm.yPwm--;
    bRestart = 1;
    user_pwm_change_duty(LED_YELLOW_NUM, ledParm.yPwm);
  }


  if(bRestart){
    // printf("time off again\n");
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6);
  }else{
    // printf("time off stop\n");
    user_pwm_stop(LED_WHITE_NUM);
    user_pwm_stop(LED_YELLOW_NUM);
  }
}

void timeLedModeChange(void){
  uni_u8 bRestart = 0;

  //0:w 1:y 2:w & y
  // printf("timeLedModeChange %d %d \n", ledParm.wPwm, ledParm.yPwm);
  
  switch(ledParm.ledMode){                  //0:w 1:y 2:w & y
    case MODE_WHITE:
      if(ledParm.wPwm==ledParm.pwmMaster){

      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        ledParm.wPwm--;
        bRestart = 1;
      }

      if(ledParm.yPwm){
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    case MODE_YELLOW: //0:w 1:y 2:w & y
      if(ledParm.wPwm){
        ledParm.wPwm--;
        bRestart = 1;
      }

      if(ledParm.yPwm==ledParm.pwmMaster){

      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    case MODE_NATURE://0:w 1:y 2:w & y
      if(ledParm.wPwm==ledParm.pwmMaster){

      }else if(ledParm.wPwm<ledParm.pwmMaster){
        ledParm.wPwm++;
        bRestart = 1;
      }else{
        ledParm.wPwm--;
        bRestart = 1;
      }

      if(ledParm.yPwm==ledParm.pwmMaster){

      }else if(ledParm.yPwm<ledParm.pwmMaster){
        ledParm.yPwm++;
        bRestart = 1;
      }else{
        ledParm.yPwm--;
        bRestart = 1;
      }
      break;

    default:
      return;
  }

  user_pwm_change_duty(LED_YELLOW_NUM, ledParm.yPwm);
  user_pwm_change_duty(LED_WHITE_NUM, ledParm.wPwm);

  if(bRestart){
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6);  
  }else{
    ledParm.modeChange = 0;
  }
}
#if 0
void timeLedLong(void){
  uni_u8 restart = 0;

  // printf("timeLedLong\n");

  if(ledParm.lumeDir){
    ledParm.pwmMaster +=1;

    // printf("add %d %d\n", ledParm.pwmMaster, MAX_PWM);

    if(ledParm.pwmMaster >MAX_PWM){
      ledParm.pwmMaster = MAX_PWM;
      // printf("small\n");
    }else{
      restart = 1;
      // printf("big\n");
    }
  }else{
    if(ledParm.pwmMaster>MIN_PWM+1){
      ledParm.pwmMaster -=1;
      // printf("sub small\n");
      restart = 1;
    }else{
      ledParm.pwmMaster = MIN_PWM;
      // printf("sub big\n");
    }
    // printf("add %d\n", ledParm.pwmMaster);
  }

  switch(ledParm.ledMode){        //0:w 1:y 2:w & y
    case 0:
      user_pwm_change_duty(LED_WHITE_NUM, ledParm.pwmMaster);
      break;

    case 1:
      user_pwm_change_duty(LED_YELLOW_NUM, ledParm.pwmMaster);
      break;

    case 2:
      user_pwm_change_duty(LED_WHITE_NUM, ledParm.pwmMaster);
      user_pwm_change_duty(LED_YELLOW_NUM, ledParm.pwmMaster);
      break;

    default:
      return;
  }

  if(restart){
    // printf("again\n");
    uni_hal_timer_pause(TIMER6, 1);
    uni_hal_timer_start(TIMER6); 
  }
}
#endif
void _timer_cb(TIMER_INDEX TimerIdx){
  if(TimerIdx == TIMER5){   //1s
    // timeKeepEye();
  }else if(TimerIdx == TIMER6){
    //led
    if(ledParm.ledStatus){
      if(ledParm.modeChange){
        timeLedModeChange();
      }else if(ledParm.bLume){
        timeLedLume();
      }else if(ledParm.bBreathe){
        timeLedBreathe();
      }else{
        timeLedOn();
      }
    }else{
      timeLedOff();
    }
  }
}

int ledInit(void){

  ledParm.lumeDir = 0;
  ledParm.ledStatus = 0;
  ledParm.ledModeLast = 0;
  ledParm.pwmMaster = 20;
  ledParm.ledMode = MODE_NATURE;

  if (0 != user_pwm_init(LED_YELLOW_NUM, 40000, true)){
    LOGE(TAG, "user_pwm_init failed.");
    return -1;
  }

  if (0 != user_pwm_init(LED_WHITE_NUM, 40000, true)){
    LOGE(TAG, "user_pwm_init failed.");
    return -1;
  }
  return 0;
}

void setLedLume(uni_u8 lumen){
  
  ledParm.bLume = 1;
  ledParm.lsLumen = ledParm.lumen;
  ledParm.lumen = lumen;
  uni_hal_timer_pause(TIMER6, 1);
  uni_hal_timer_start(TIMER6);
  printf("setLedLume %d\n", ledParm.pwmMaster);
}

void setLedAllOff(void){

  printf("setLedAllOff\n");
  ledParm.lsLumen = ledParm.lumen;
  ledParm.lumen = MAX_PWM;

  uni_hal_timer_pause(TIMER6, 1);
  uni_hal_timer_start(TIMER6);
}

void setLedModeChange(void){
  printf("setLedModeChange to mode:%d\n", ledParm.ledMode);
  ledParm.modeChange = 1;

  uni_hal_timer_pause(TIMER6, 1);
  uni_hal_timer_start(TIMER6);
}

void setLedModeBreathe(void)
{
  /*
    在当前灯光模式下做呼吸灯效果
  */
  printf("setLedModeBreathe\n");
  ledParm.bBreathe = 1;
}

void setLedAllOn(void){
  
  ledParm.ledMode = ledParm.ledModeLast;
  ledParm.pwmMaster = ledParm.lsLumen;
  ledParm.lumen = ledParm.lsLumen;
  printf("setLedAllOn mode:%d\n", ledParm.ledMode); 

  switch(ledParm.ledMode){    //0:w 1:y 2:w & y
    case MODE_WHITE:
      user_pwm_start(LED_WHITE_NUM, 0);
      user_pwm_start(LED_YELLOW_NUM, 0);
      break;

    case MODE_YELLOW:
      user_pwm_start(LED_WHITE_NUM, 0);
      user_pwm_start(LED_YELLOW_NUM, 0);
      break;

    case MODE_NATURE:
      user_pwm_start(LED_WHITE_NUM, 0);
      user_pwm_start(LED_YELLOW_NUM, 0);
      break;
    default:
      return;
  }

  uni_hal_timer_pause(TIMER6, 1);
  uni_hal_timer_start(TIMER6);
}

//===touch key
void touchKeyCb(GPIO_NUMBER num, uni_bool is_high){
  static unsigned char bMode = 0;
  
  if(cntTkDelay){
    return;
  }
  cntTkDelay = 20;
  
  switch(num){
    case KEY_SW_NUM:
      
      if(!is_high){
        printf("key dowm\n");
        // ledParm.bVoiceEn = 0;

        if(ledParm.ledStatus){
          ledParm.ledMode++;
          if(ledParm.ledMode<=MODE_NATURE){
            ledParm.ledModeLast = ledParm.ledMode;
            setLedModeChange();
            user_flash_set();
          }else{
            ledParm.ledMode = MODE_WHITE;
            ledParm.ledModeLast = ledParm.ledMode;
            ledParm.ledStatus = 0;
            setLedAllOff();
          }

        }else{
          ledParm.ledStatus = 1;
          setLedAllOn();
        }
      }else{
        printf("key up\n");
      }
      break;

    default:break;
  }
}

void touchKeyInit(void){
  user_gpio_set_mode(KEY_SW_NUM, GPIO_MODE_IN);
  user_gpio_set_pull_mode(KEY_SW_NUM, GPIO_PULL_UP);

  user_gpio_set_interrupt(KEY_SW_NUM, GPIO_INT_BOTH_EDGE, touchKeyCb);
  user_gpio_interrupt_enable();
}

void touchKeyScan(void){
  if(cntTkDelay){
    cntTkDelay--;
  }
}

//--------------------------------------

static void ledCrt(void *args){
  while(1){
    touchKeyScan();

    uni_msleep(1);
  }
}
 
int user_flash_get(void)
{
  BLOB_TYPE_U32 tmp = 0;
  BLOB_TYPE_U32 save_len = 0;
  printf("user_flash_get \n");

  user_flash_init();

  user_flash_get_env_blob(BLOB_COLOR, &tmp, sizeof(BLOB_TYPE_U32), &save_len);
  printf("ger flash %d\n", save_len);

  if(tmp<3){
    ledParm.ledModeLast = tmp;
    ledParm.ledMode = tmp;
  }else{
    ledParm.ledMode = 0;
    ledParm.ledModeLast = 0;
    tmp = ledParm.ledMode;
    user_flash_set_env_blob(BLOB_COLOR, &tmp, sizeof(BLOB_TYPE_U32));
  }
  user_flash_get_env_blob(BLOB_LUMEN, &tmp, sizeof(BLOB_TYPE_U32), &save_len);
  printf("ger flash %d %d\n", save_len, tmp);
  if(tmp<=MAX_PWM && tmp>=MIN_PWM){
    ledParm.lsLumen = tmp;
    ledParm.lumen = tmp;
  }else{
    ledParm.lumen = MIN_PWM;
    ledParm.lsLumen = MIN_PWM;
    tmp = ledParm.lumen;
    user_flash_set_env_blob(BLOB_LUMEN, &tmp, sizeof(BLOB_TYPE_U32));
  }

  printf("user flash read lumen:%d last lumun:%d mode:%d\n", ledParm.lumen, ledParm.lsLumen, ledParm.ledMode);
}

int user_flash_set(void)
{
  BLOB_TYPE_U32 tmp = 0;
  uni_u32 size = 0;

  user_flash_init();

  tmp = (uni_u32)ledParm.ledMode;
  user_flash_set_env_blob(BLOB_COLOR, &tmp, sizeof(BLOB_TYPE_U32));
  tmp = (uni_u32)ledParm.lumen;
  size = user_flash_set_env_blob(BLOB_LUMEN, &tmp, sizeof(BLOB_TYPE_U32));
  printf("user flash write %d %d %d\n", ledParm.lumen, ledParm.ledMode, size);
}

int user_parm_init(void)
{

  ledParm.wPwm = 0;
  ledParm.yPwm = 0;
  user_flash_get();
  ledParm.lsLumen = 100;//不读取flash参数
  ledParm.lumen = 100;
  return 1;
}

int hb_auto_gpio(void){
  user_gpio_init();
  user_gpio_set_mode(GPIO_NUM_A25, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A25, 1);
  user_gpio_set_mode(GPIO_NUM_A26, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_A26, 1);
  user_gpio_set_mode(GPIO_NUM_B2, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_B2, 0);
  user_gpio_set_mode(GPIO_NUM_B3, GPIO_MODE_OUT);
  user_gpio_set_value(GPIO_NUM_B3, 0);

  // user_gpio_set_mode(LED_MODE_NUM, GPIO_MODE_OUT);
  // user_gpio_set_value(LED_MODE_NUM, 1);
  // user_gpio_set_mode(LED_POWER_NUM, GPIO_MODE_OUT);
  // user_gpio_set_value(LED_POWER_NUM, 1);

  _register_event_callback();

  //新建一个线程  作为串口协议  连续返送回调处理
  uni_pthread_t pid;
  thread_param param;
  param.stack_size = STACK_SMALL_SIZE;
  param.priority   = OS_PRIORITY_HIGH;
  strncpy(param.task_name, "ledCrt", sizeof(param.task_name) - 1);
  uni_pthread_create(&pid, &param, ledCrt, NULL);       //传入参数  作为语音播报用  
  uni_pthread_detach(pid);

  user_parm_init();
  ledInit();
  // touchKeyInit();

  uni_hal_timer_init(TIMER5, 1*1000*1000, true, _timer_cb);//1s
  uni_hal_timer_init(TIMER6, 5*1000, true, _timer_cb);//20ms


  user_pwm_stop(LED_WHITE_NUM);
  user_pwm_stop(LED_YELLOW_NUM);

  return 0;
}




